using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using TciNewsReader_2014_04.Model;
namespace TciNewsReader_2014_04.VmHelpers
{
    public sealed class CrudWrapper : INotifyPropertyChanged, IEditableObject
    {
        private static readonly UriHelpers UriHelpers = new UriHelpers();
        private CrudState _state = CrudState.Unchanged;
        public CrudState State
        {
            get { return _state; }
            set
            {
                if (value != _state)
                {
                    _state = value;
                    OnPropertyChanged("State");
                }
            }
        }
        private UriVerificationResult _uriVerificationResult = UriVerificationResult.Pending;
        public UriVerificationResult UriVerificationResult
        {
            [DebuggerStepThrough]
            get { return _uriVerificationResult; }
            [DebuggerStepThrough]
            set
            {
                if (value != _uriVerificationResult)
                {
                    _uriVerificationResult = value;
                    OnPropertyChanged("UriVerificationResult");
                }
            }
        }
        public string ValidationResult { get; private set; }
        public bool PassedValidation { get; private set; }
        private string _url;
        public string Url
        {
            get { return _url; }
            set
            {
                if (value != _url)
                {
                    if (!String.IsNullOrEmpty(_url))
                    {
                        State = CrudState.Updated;
                    }
                    _url = value;
                    OnPropertyChanged("Url");
                    string result;
                    if (UriHelpers.TryValidateUri(_url, out result))
                    {
                        ValidationResult = "Ok";
                        PassedValidation = true;
                    }
                    else
                    {
                        ValidationResult = result;
                        PassedValidation = false;
                    }
                    OnPropertyChanged("ValidationResult");
                    VerificationResponseTime = String.Empty;
                    UriVerificationResult = UriVerificationResult.Pending;
                }
            }
        }
        private string _verificationResponseTime;
        public string VerificationResponseTime
        {
            [DebuggerStepThrough]
            get { return _verificationResponseTime; }
            [DebuggerStepThrough]
            set
            {
                if (value != _verificationResponseTime)
                {
                    _verificationResponseTime = value;
                    OnPropertyChanged("VerificationResponseTime");
                }
            }
        }
        public void Verify()
        {
            if (!PassedValidation)
            {
                VerificationResponseTime = "Not attempted";
                return;
            }
            try
            {
                DateTime start = DateTime.Now;
                using (WebClient wc = new WebClient())
                {
                    wc.DownloadString(Url);
                }
                DateTime end = DateTime.Now;
                TimeSpan ts = end - start;
                VerificationResponseTime = String.Format("Ok - {0} seconds", ts.TotalSeconds.ToString("####,##0.000"));
                UriVerificationResult = UriVerificationResult.Ok;
            }
            catch (Exception ex)
            {
                VerificationResponseTime = ex.Message;
                UriVerificationResult = UriVerificationResult.Failed;
            }
        }
        public bool CanUseThisUrl()
        {
            return PassedValidation && UriVerificationResult == UriVerificationResult.Ok;
        }
        #region INotifyPropertyChanged Implementation
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name)
        {
            var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion
        #region IEditableObject
        public void BeginEdit(){}
        public void CancelEdit(){}
        public void EndEdit()
        {
            if (Url == null)
            {
                Url = String.Empty;
            }
            Url = Url.Trim();
        }
        #endregion
    }
}